<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	public function setOptions(array $options)
	{
		if(isset($options[ 'module' ])){
			$request = new Zend_Controller_Request_Http();
			$moduleResources = $options[ 'module' ];
			$moduleName = ltrim(str_replace($request->getBaseUrl(), '', $request->getRequestUri()),'/');
			if(false !== ($len = strpos($moduleName, '/'))){
				$moduleName = substr($moduleName,0,$len);
			}
			$moduleName = ($moduleName) ? $moduleName : 'default';
			if(isset($moduleResources[ $moduleName ])){
				$moduleResources = $moduleResources[ $moduleName ];
			}else{
				$moduleResources = isset( $moduleResources[ 'default' ] ) ? $moduleResources[ 'default' ] : array( );
			}
			
			if(!isset($moduleResources[ 'resources' ])){
				$moduleResources[ 'resources' ] = array( );
			}
			if(!isset($options[ 'resources' ])){
				$options[ 'resources' ] = array( );
			}
			$options[ 'resources' ] = array_merge_recursive($options[ 'resources' ],$moduleResources[ 'resources' ]);
			unset( $options[ 'module' ] );
		}
		parent::setOptions($options);
	}
	
	public function _initModuleLoaders()
    {
        $this->bootstrap('Frontcontroller');

        $fc = $this->getResource('Frontcontroller');
        $modules = $fc->getControllerDirectory();
		
        foreach ($modules AS $module => $dir) {
            $moduleName = strtolower($module);
            $moduleName = str_replace(array('-', '.'), ' ', $moduleName);
            $moduleName = ucwords($moduleName);
            $moduleName = str_replace(' ', '', $moduleName);

            $loader = new Zend_Application_Module_Autoloader(array(
                'namespace' => $moduleName,
                'basePath' => realpath($dir . "/../"),
            ));
        }
    }

}

